package top.lyc829.zzl_android.ui.message.applay;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ActivityNoticeApplyBinding;
import top.lyc829.zzl_android.logic.bean.NoticeCommentBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;
import top.lyc829.zzl_android.ui.message.follow.NoticeFollowAdapter;
import top.lyc829.zzl_android.ui.message.follow.NoticeFollowViewModel;
import top.lyc829.zzl_android.ui.profile.follow.user_detail.OtherUserDetailActivity;

public class NoticeApplyActivity extends AppCompatActivity {

    ActivityNoticeApplyBinding binding;
    NoticeApplyViewModel mViewModel;
    NoticeApplyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoticeApplyBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.mTitleBarApplyApply.setLeftClickListener(v -> finish());

        mViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoticeApplyViewModel.class);

        initRecycler();
        observer();
        clickListener();

    }


    private void clickListener() {
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            NoticeCommentBean comment = adapter.getItem(position);
            Intent intent = new Intent(this, OtherUserDetailActivity.class);
            intent.putExtra("id", comment.getUserId());
            startActivity(intent);
        });

        adapter.addChildClickViewIds(R.id.btn_apply_agree, R.id.btn_apply_reject);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {

            int userId = adapter.getItem(position).getUserId();
            if (view.getId() == R.id.btn_apply_agree) {
                handleUserApply(Utils.getUserId(), userId, 1, adapter.getItem(position).getSourceId(), position);
            } else if (view.getId() == R.id.btn_apply_reject) {
                handleUserApply(Utils.getUserId(), userId, 0,adapter.getItem(position).getSourceId(), position);
            }
        });


    }

    private void handleUserApply(int userId, int applyUserId, int result, int activityId, int position) {
        Network.handleTeamApply(userId, applyUserId, result,activityId,  new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<Void> resultBean = JsonUtilNew.fromJsonObject(resStr, Void.class);
                if (resultBean.getCode().equals(200)) {
                    runOnUiThread(() -> {
                        Utils.showToast("操作成功");
                        // 这么做用户换手机后申请会丢失
                        mViewModel.deleteItem(adapter.getItem(position));
                        adapter.removeAt(position);
                    });
                } else if (resultBean.getCode().equals(402)) {
                    runOnUiThread(() -> {
                        Utils.showToast("操作失败，用户已经取消");
                        // 这么做用户换手机后申请会丢失
                        mViewModel.deleteItem(adapter.getItem(position));
                        adapter.removeAt(position);
                    });
                }

                else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.showToastSync(e.getMessage());
            }
        });
    }


    private void observer() {

        mViewModel.getNoticeApplyLiveData().observe(this, noticeCommentBeans -> {
            adapter.setList(noticeCommentBeans);
        });
    }

    private void initRecycler() {
        adapter = new NoticeApplyAdapter();
        binding.noticeApplyRecycler.setLayoutManager(new LinearLayoutManager(this));
        binding.noticeApplyRecycler.setAdapter(adapter);
        adapter.setAnimationEnable(true);   // recyclerview加载动画
    }



    @Override
    protected void onResume() {
        super.onResume();
        super.onResume();
        if (!Utils.isLogin()) {
            Utils.showToast("请先登录");
            //Utils.goLogin(this);
        } else {
            mViewModel.loadNoticeApply(Utils.getUserId());
        }
    }
}